struct Node{
Node * left;
Node * right;
int lmaxlen;
int rmaxlen;
char val;
};
int maxlen = 0;
//查找二叉树中最远的两个节点
void search(Node * root){
if(root == NULL){
return ;
}
if(root->left != NULL){
search(root->left);
}else{
root->lmaxlen = 0;
}
if(root->right != NULL){
search(root->right);
}else{
root->rmaxnlen = 0;
}
if(root->left != NULL){
int tpmaxlen = 0;
if(root->left->lmaxlen > root->left->rmaxlen){
tpmaxlen = root->left->lmaxlen;
}else{
tpmaxlen = root->left->raxlen;
}
root->lmaxlen = tpmaxlen + 1;
}
if(root->right != NULL{
int tpmaxlen = 0;
if(root->right->rmaxlen > root->right->lmaxlen){
tpmaxlen = root->right->rmaxlen;
}else{
tpmaxlen = root->right->lmaxlen;
}
root->lmaxlen = tpmaxlen + 1;
}
if(root->lmaxlen + root->rmaxlen > maxlen){
maxlen = root->lmaxlen + root->rmaxlen;
}
}
查找二叉树最远两个节点的距离
最新推荐文章于 2019-06-11 19:09:42 发布